package cz.possoft.client.clovece.gui.components;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.Map;

import cz.possoft.client.clovece.gui.panel.ClovecePanel;
import cz.possoft.client.utils.PanelHolderUtils;
import cz.possoft.generic.utils.GenericUtils;
import cz.possoft.serializable.clovece.game.CloveceField;
import cz.possoft.serializable.generic.Location;

/**
 * @author <a href="mailto:mposolda@gmail.com">Marek Posolda</a>
 */
class ClovecePaneListener extends MouseAdapter
{
   
   /**
    * This happen when mouse is moved in the space under the pane.
    * 
    * TODO: remove this method...
    */
   public void mouseMoved(MouseEvent e) 
   {
////      System.out.println("MOUSE MOVED");
//      if (!(PanelHolderUtils.getCurrentGamePanel() instanceof ClovecePanel)) 
//      {
//         return;
//      }
//      ClovecePanel clovecePanel = (ClovecePanel)PanelHolderUtils.getCurrentGamePanel();
//      
//      if (clovecePanel.getStateOfGame() == 2)
//      {
//         CloveceFigurkaOnPane clf = getCloveceFigurka(e);
////         if (clf != null) && (clf.getRingColor() !=)
////         {
////            // random color different from neutral (For example white)
////            clf.setRingColor(GenericUtils.COLOR_WHITE);
////            e.getComponent().repaint();
////         }
//      }
   }
   
   /**
    * This happen when mouse is exited from space under the pane.
    */      
   public void mouseExited(MouseEvent e) 
   {         
      
   }      
   
   /**
    * This happen when mouse is pressed under the pane.
    */
   public void mousePressed(MouseEvent e) 
   {
      if (!(PanelHolderUtils.getCurrentGamePanel() instanceof ClovecePanel)) 
      {
         return;
      }
      final ClovecePanel clovecePanel = (ClovecePanel)PanelHolderUtils.getCurrentGamePanel();
      ClovecePane clPane = clovecePanel.getGamePane();
      
      int colorNaTahu = clovecePanel.getGame().isWhiteNaTahu() ? GenericUtils.COLOR_WHITE : GenericUtils.COLOR_BLACK;
      
      if (clovecePanel.getStateOfGame() == 2)
      {
            CloveceFigurkaOnPane selected = getCloveceFigurka(e);            
            
            if ((selected != null) && (selected.getRingColor() != GenericUtils.COLOR_NEUTRAL))
            {
               System.out.println("Selecting ring figurka for play: " + selected);
                              
               // some color different from neutral
               selected.setRingSelectedForPlay(GenericUtils.COLOR_WHITE);
               clPane.setSelectedFigurkaForPlay(selected);
               
               // Delete all rings from previous state.
               clPane.clearRings();                              

               // Now ring fields will be fields where we can play.
               Collection<CloveceField> fields = clovecePanel.getGame().getHraciPole().getFieldsWhereFigurkaCanPlay(selected.getField(), clovecePanel.getCubePane().getStateOnCube(), colorNaTahu);
               for (CloveceField field : fields)
               {
                  CloveceFigurkaOnPane clFigurka = clPane.getFigurkaOnPaneById(field.getId());
                  clFigurka.setRingForPlaceWhereWeCanPlay(colorNaTahu);
               }
               
               e.getComponent().repaint();
            }
      } 
      
      // this means that we will try to play with our figurka.
      else if (clovecePanel.getStateOfGame() == 3)
      {
         CloveceFigurkaOnPane figurkaTo = getCloveceFigurka(e);  
         clovecePanel.doValidationAndPlayWithFigurka(figurkaTo);
      }
   }  
   
   private CloveceFigurkaOnPane getCloveceFigurka(MouseEvent me)
   {      
      int x = me.getX();
      int y = me.getY();
      
      // Compute location on the board where is mouse
      int newX = (-21 + x) / 36;
      int newY = (-21 + y) / 36;
      
      // Check if we are in the circle. Return if not.
      int stredX = 42 + 36*newX;
      int stredY = 42 + 36*newY;
      int vzdX = x-stredX;
      int vzdY = y-stredY;            
      if ((vzdX*vzdX + vzdY*vzdY) > 170)
      {
         return null;
      }
                  
      Location ourLoc = new Location(newX, newY);
      System.out.println("Location: " + ourLoc);
      
      // check if there is some field in our location
      CloveceFigurkaOnPane result = getCloveceFigurkaOnPaneForCollection(CloveceFigurkaOnPane.BASIC_FIELD_LOCATIONS, ourLoc, me, 0);
      if (result == null)
      {
         result = getCloveceFigurkaOnPaneForCollection(CloveceFigurkaOnPane.WHITE_PLAYER_HOME_FIELD_LOCATIONS, ourLoc, me, 100);
      }
      if (result == null)
      {
         result = getCloveceFigurkaOnPaneForCollection(CloveceFigurkaOnPane.BLACK_PLAYER_HOME_FIELD_LOCATIONS, ourLoc, me, 200);
      }
      if (result == null)
      {
         result = getCloveceFigurkaOnPaneForCollection(CloveceFigurkaOnPane.WHITE_PLAYER_START_FIELD_LOCATIONS, ourLoc, me, 1000);
      }
      if (result == null)
      {
         result = getCloveceFigurkaOnPaneForCollection(CloveceFigurkaOnPane.BLACK_PLAYER_START_FIELD_LOCATIONS, ourLoc, me, 2000);
      }  
            
      return result;      
   }
   
   private CloveceFigurkaOnPane getCloveceFigurkaOnPaneForCollection(Map<Integer, Location> locations, Location ourLoc, MouseEvent me, int idPrefix)
   {
      for (Map.Entry<Integer, Location> entry : locations.entrySet())
      {
         if (ourLoc.equals(entry.getValue()))
         {
            return ((ClovecePane)me.getComponent()).getFigurkaOnPaneById(entry.getKey() + idPrefix);
         }
      } 
      
      return null;
   }

}

